
gsea_kegg <- readRDS(file.path(gsea_od,"gsea_kegg.rds"))
gsea_kegg %>% as.data.frame() %>% arrange(NES) %>% .[["Description"]] %>% str_remove_all('KEGG_') %>% 
    str_replace_all('_',' ')

write_tsv(gsea_kegg %>% as.data.frame() ,file = file.path(gsea_od,"gsea_kegg.xls"))

f <- \(x){
    p_gsea_kegg_line <- enrichplot::gseaplot2(
        x = gsea_kegg,
        geneSetID = gsea_kegg %>% filter(Description == x) %>% as.data.frame() %>% pull(ID), # 只显示前4个GSEA的结果
        title = NULL, # 标题
        color = "#4daf4a", # 颜色
        pvalue_table = FALSE,
        ES_geom = "line",
        rel_heights = c(1, 0.2, .4)
    )
    df <- as.data.frame(gsea_kegg %>% filter(Description == x))
    p_chara <- ifelse(df$p.adjust < 0.001, "< 0.001", sprintf(" = %.3f", df$p.adjust))

    y_pos <- ifelse(df$NES > 0,.3,-.1)

    annote_chara <- str_glue("{df$Description}\nNES = {sprintf(\"%.3f\",df$NES)}\np.adjust{p_chara}")
    p_gsea_kegg_line[[1]] <- p_gsea_kegg_line[[1]] + annotate(
        geom = "text", x = 500, y = y_pos,
        label = annote_chara, size = 4, hjust = "left"
    )

    plotout(
        p = p_gsea_kegg_line, od = paste0(gsea_od),
        name = str_glue("gsea_{x}"), w = 4.5, h = 4.2
    )

    return(p_gsea_kegg_line)
}

p_lst <- map(c("KEGG_MTOR_SIGNALING_PATHWAY","KEGG_TGF_BETA_SIGNALING_PATHWAY"), f)

p <- ggpubr::ggarrange(plotlist = p_lst, nrow = 2, ncol = 1)
plotout(
    p = p, od = paste0(gsea_od),
    name = str_glue("gsea"), w = 4.2, h = 7
)
